package com.gongpingjialoan.activity.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.data.SellPrivateCarBean;

/**
 * Created by Administrator on 2016/5/30.
 */
public class SelectAddressActivity extends BaseActivity {

    final int SELECTADDRESS = 101;

    TextView addressTitleT;
    EditText addressDetailE;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_address);
        initView();
    }

    private void initView() {
        setTitle("具体地址");
        addressTitleT = (TextView) findViewById(R.id.address_title);
        addressDetailE = (EditText) findViewById(R.id.address_detail);
        SellPrivateCarBean sellPrivateCarBean = SellPrivateCarBean.getInstance();
        if(!TextUtils.isEmpty(sellPrivateCarBean.address_title)){
            addressTitleT.setText(sellPrivateCarBean.address_title);
        }
        if(!TextUtils.isEmpty(sellPrivateCarBean.address)){
            addressDetailE.setText(sellPrivateCarBean.address);
        }
        addressTitleT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(SelectAddressActivity.this, MapActivity.class);
                startActivityForResult(it, SELECTADDRESS);
            }
        });

        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("小区/写字楼/学校等".equals(addressTitleT.getText().toString())) {
                    Toast.makeText(SelectAddressActivity.this, "请选择地址!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(addressDetailE.getText().toString())) {
                    Toast.makeText(SelectAddressActivity.this, "请输入详细地址!", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent it = new Intent();

                it.putExtra("address_title", addressTitleT.getText().toString());
                it.putExtra("address_detail", addressDetailE.getText().toString());
                setResult(Activity.RESULT_OK, it);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECTADDRESS && resultCode == Activity.RESULT_OK) {
            String title = data.getStringExtra("title");
            String detail = data.getStringExtra("detail");
            addressTitleT.setText(title);
            addressTitleT.setTextColor(getResources().getColor(R.color.discount_price_other_color));
            addressDetailE.setText(detail);
        }
    }
}
